package com.daisuxia.web.spider;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;

/**
 * 通过url获取返回结果
 * 
 * @author simalin
 * 
 */
public class HttpUtil {

	private static Logger loger = Logger.getLogger(HttpUtil.class);

	/**
	 * paraMap:{debug=, type=test, name=55cc6c83a1bb0, time=1439460486,
	 * md5=4e5a404791cebbc32b1f93b65448765c} String URL =
	 * "http://api.haitungongyinglian.hk";
	 * 
	 * @param url
	 * @return
	 */
	public static String postSends(String url) {
		loger.info("----httpclient:--->" + url);
		try {
			HttpClient httpClient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(url);
			List<NameValuePair> parameters = new ArrayList<NameValuePair>();
			parameters.add(new BasicNameValuePair("type", "test"));
			parameters.add(new BasicNameValuePair("name", "55cc6c83a1bb0"));
			parameters.add(new BasicNameValuePair("time", "1439462710479"));
			parameters.add(new BasicNameValuePair("md5",
					"4e5a404791cebbc32b1f93b65448765c"));
//			System.out.println("parameters=" + parameters.toString());
			httpPost.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
			HttpResponse httpResponse = httpClient.execute(httpPost);
			String result = gainContent(httpResponse);
//			System.out.println("result=" + result);
			httpClient.getConnectionManager().shutdown();
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String getSends(String url) {
		loger.info("----httpclient:--->" + url);
		try {
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(url);
			HttpResponse httpResponse = httpClient.execute(httpGet);
			String result = gainContent(httpResponse);
//			System.out.println("result=" + result);
			httpClient.getConnectionManager().shutdown();
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String gainContent(HttpResponse httpResponse)
			throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				httpResponse.getEntity().getContent()));
		String resultStr = reader.readLine();
//		System.out.println("resultStr=" + resultStr);
		String result = "";
		while (null != resultStr) {
			result = resultStr;
//			System.out.println("re=" + result);
			resultStr = reader.readLine();
			return result;
		}
		return "";
	}

	public static void main(String[] args) {
		String URL = "http://api.haitungongyinglian.hk?type=test&name=55cc6c83a1bb0&time=1439462048491&md5=4e5a404791cebbc32b1f93b65448765c";
		String urls = "http://api.haitungongyinglian.hk";
		// http://api.haitungongyinglian.hk?type=test&name=55cc6c83a1bb0&time=1439462048491&md5=4e5a404791cebbc32b1f93b65448765c
		Date date = new Date();
//		System.out.println(date.getTime());
		postSends(urls);
		// getSends(URL);
	}

}
